package com.ruoyi.ruoyiyinzheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ruoyiyinzheng.domain.bo.StRecieptNameBo;
import com.ruoyi.ruoyiyinzheng.domain.vo.StRecieptNameVo;
import com.ruoyi.ruoyiyinzheng.domain.StRecieptName;
import com.ruoyi.ruoyiyinzheng.mapper.StRecieptNameMapper;
import com.ruoyi.ruoyiyinzheng.service.IStRecieptNameService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 配方名称Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-23
 */
@RequiredArgsConstructor
@Service
public class StRecieptNameServiceImpl implements IStRecieptNameService {

    private final StRecieptNameMapper baseMapper;

    /**
     * 查询配方名称
     */
    @Override
    public StRecieptNameVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询配方名称列表
     */
    @Override
    public TableDataInfo<StRecieptNameVo> queryPageList(StRecieptNameBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StRecieptName> lqw = buildQueryWrapper(bo);
        Page<StRecieptNameVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询配方名称列表
     */
    @Override
    public List<StRecieptNameVo> queryList(StRecieptNameBo bo) {
        LambdaQueryWrapper<StRecieptName> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StRecieptName> buildQueryWrapper(StRecieptNameBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StRecieptName> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getStReceiptName()), StRecieptName::getStReceiptName, bo.getStReceiptName());
        lqw.eq(StringUtils.isNotBlank(bo.getReciptCode()), StRecieptName::getReciptCode, bo.getReciptCode());
        lqw.eq(StringUtils.isNotBlank(bo.getEnable()), StRecieptName::getEnable, bo.getEnable());
        return lqw;
    }

    /**
     * 新增配方名称
     */
    @Override
    public Boolean insertByBo(StRecieptNameBo bo) {
        StRecieptName add = BeanUtil.toBean(bo, StRecieptName.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改配方名称
     */
    @Override
    public Boolean updateByBo(StRecieptNameBo bo) {
        StRecieptName update = BeanUtil.toBean(bo, StRecieptName.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StRecieptName entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除配方名称
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
